package com.ffcs.web.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.ffcs.web.exceptions.SystemException;




/**
 * @Title: Sha1Util.java 
 * @Package util 
 * @Description: IMS的SHA1算法
 * @Copyright: Copyright (c) 2010 FFCS All Rights Reserved
 * @author 俞建龙
 * @date 2013-10-23 上午10:06:54 
 * @version V1.00.00 
 */
public class Sha1Util2 {
	public static byte[] sha1Encrypt(String content)  {
	    MessageDigest alg;
		try {
			alg = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			throw SystemException.wrap(e)
							.set("Cause", "get MessageDigest instance")
							.set("Algorithm", "SHA-1");
		}
	    alg.update(content.getBytes());
	    byte[] bts = alg.digest();
//	    String result = "";
//	    String tmp = "";
//	    for (int i = 0; i < bts.length; i++) {
//	        tmp = (Integer.toHexString(bts[i] & 0xFF));
//	        if (tmp.length() == 1) result += "0";
//	          result += tmp;
//	    }
	    return bts;
	}
	public static String sha1EncryptStr(String content)  {
	    MessageDigest alg;
		try {
			alg = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			throw SystemException.wrap(e)
							.set("Cause", "get MessageDigest instance")
							.set("Algorithm", "SHA-1");
		}
	    alg.update(content.getBytes());
	    byte[] bts = alg.digest();
	    String result = "";
	    String tmp = "";
	    for (int i = 0; i < bts.length; i++) {
	        tmp = (Integer.toHexString(bts[i] & 0xFF));
	        if (tmp.length() == 1) result += "0";
	          result += tmp;
	    }
	    return result;
	}
}

